package pl.mdziedzic.unittests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static pl.mdziedzic.unittests.CreateAuctionRequest.createBuyNowAuction;

import java.util.Calendar;

import org.junit.Before;
import org.junit.Test;

import pl.mdziedzic.unittests.services.RequestContext;

public class AuctionTest2 {
	
	private static final String AUCTIONNAME = "Jan Kowalski";
	private static final long PRIZE = 1000;
	
	private RequestContext context;
	private Calendar startTime;
	private Calendar endTime;
	private User user;
	private CreateAuctionRequest request;


	@Before
	public void createAuctionFields() {		
		user = new User("example.user");
		
		context = new RequestContext(user);
		
		startTime = Calendar.getInstance();
		startTime.add(Calendar.DATE, 1);
		
		endTime = (Calendar) startTime.clone();
		endTime.add(Calendar.DATE, 7);

		request = createBuyNowAuction(AUCTIONNAME, startTime, endTime, PRIZE);
	}

	
	@Test
	public void shouldCreateBuyNowAuction() {
		//when
		Auction auction = new Auction(context, request);

		//then
		assertBuyNowAuction(auction);
	}
	

	private void assertBuyNowAuction(Auction auction) {
		assertFalse(auction.hasBids());
		assertEquals("Jan Kowalski", auction.getName());
		assertEquals(startTime, auction.getStartTime());
		assertEquals(endTime, auction.getEndTime());
		assertEquals(AUCTION_TYPE.BUY_NOW, auction.getType());
		assertEquals(AUCTION_STATUS.NEW, auction.getStatus());
		assertEquals(PRIZE, auction.getBuyNowPrice());
		assertEquals(user, auction.getOwner());
	}

	
	@Test
	public void shouldActivateCampaign() {
		//given
		Auction auction = new Auction(context, request);

		Calendar now = context.getNow();
		now.add(Calendar.DATE, 1);
		now.add(Calendar.SECOND, 1);
		
		//when
		auction.activate();

		//then
		assertNotNull(auction.getActivateDate());
		assertEquals(AUCTION_STATUS.ACTIVE, auction.getStatus());
	}
	
}
